package aPlusContentV20201101

import (
	"encoding/json"
	"errors"
	"fmt"
	"gitee.com/HuaLiLiDeDiDiao/amazon-spapi-sdk-go/client"
	"net/url"
)

func SearchContentDocuments(spClient *client.SPAPIClient, request SearchContentDocumentsRequest) (*SearchContentDocumentsResponse, error) {
	// 构建 API 路径
	path := "/aplus/2020-11-01/contentDocuments"

	// 构建 Query 参数
	queryParams := url.Values{}
	if len(request.MarketplaceId) > 0 {
		queryParams.Add("marketplaceId", request.MarketplaceId)
	}
	if request.PageToken != "" {
		queryParams.Add("pageToken", request.PageToken)
	}
	// 调用通用请求方法
	resp, err := spClient.MakeRequest("GET", path, queryParams, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get reports: %w", err)
	}
	// 解析响应数据
	var response SearchContentDocumentsResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}

// CreateContentDocument 创建内容文档
func CreateContentDocument(spClient *client.SPAPIClient, marketPlaceId string, request PostContentDocumentRequest) (*PostContentDocumentResponse, error) {
	// 构建 API 路径
	path := "/aplus/2020-11-01/contentDocuments"
	// 构建 Query 参数
	queryParams := url.Values{}
	if marketPlaceId == "" {
		return nil, fmt.Errorf("marketPlaceId is required")
	}
	queryParams.Add("marketplaceId", marketPlaceId)
	// 调用 SP-API 请求
	resp, err := spClient.MakeRequest("POST", path, nil, request)
	if err != nil {
		return nil, fmt.Errorf("failed to create content document: %w", err)
	}
	// 解析响应数据
	var response PostContentDocumentResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}

// GetContentDocument 获取内容文档
func GetContentDocument(spClient *client.SPAPIClient, contentReferenceKey string, marketPlaceId string, includedDataSet string) (*GetContentDocumentResponse, error) {
	if contentReferenceKey == "" {
		return nil, errors.New("contentReferenceKey is empty")
	}
	// 构建 API 路径
	path := fmt.Sprintf("/aplus/2020-11-01/contentDocuments/%s", contentReferenceKey)
	queryParams := url.Values{}
	if marketPlaceId == "" {
		return nil, fmt.Errorf("marketPlaceId is empty")
	}
	queryParams.Add("marketplaceId", marketPlaceId)
	queryParams.Add("includedDataSet", includedDataSet)
	// 调用 SP-API 请求
	resp, err := spClient.MakeRequest("GET", path, queryParams, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get content document: %w", err)
	}
	// 解析响应数据
	var response GetContentDocumentResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}

	return &response, nil
}

// UpdateContentDocument 更新内容文档
func UpdateContentDocument(spClient *client.SPAPIClient, contentReferenceKey string, marketPlaceId string, request PostContentDocumentRequest) (*PostContentDocumentResponse, error) {
	if contentReferenceKey == "" {
		return nil, errors.New("contentReferenceKey is required")
	}
	// 构建 API 路径
	path := fmt.Sprintf("/aplus/2020-11-01/contentDocuments/%s", contentReferenceKey)
	queryParams := url.Values{}
	queryParams.Add("marketplaceId", marketPlaceId)
	// 调用 SP-API 请求
	resp, err := spClient.MakeRequest("POST", path, queryParams, request)
	if err != nil {
		return nil, fmt.Errorf("failed to update content document: %w", err)
	}
	// 解析响应数据
	var response PostContentDocumentResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}

// ListContentDocumentAsinRelations 列出内容文档与 ASIN 关系
func ListContentDocumentAsinRelations(spClient *client.SPAPIClient, contentReferenceKey string, marketPlaceId string, request ListContentDocumentAsinRelationsRequest) (*ListContentDocumentAsinRelationsResponse, error) {
	if contentReferenceKey == "" {
		return nil, errors.New("contentReferenceKey is required")
	}
	// 构建 API 路径
	path := fmt.Sprintf("/aplus/2020-11-01/contentDocuments/%s/asins", contentReferenceKey)
	// 构建 Query 参数
	queryParams := url.Values{}
	if marketPlaceId == "" {
		return nil, fmt.Errorf("marketPlaceId is required")
	}
	queryParams.Add("marketplaceId", marketPlaceId)
	if len(request.IncludedDataSet) > 0 {
		for _, set := range request.IncludedDataSet {
			queryParams.Add("includedDataSet", set)
		}
	}
	if len(request.AsinSet) > 0 {
		for _, asin := range request.AsinSet {
			queryParams.Add("asinSet", asin)
		}
	}
	if request.PageToken != "" {
		queryParams.Add("pageToken", request.PageToken)
	}
	// 调用 SP-API 请求
	resp, err := spClient.MakeRequest("GET", path, queryParams, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to list content document ASIN relations: %w", err)
	}
	fmt.Println(string(resp.Body()))
	// 解析响应数据
	var response ListContentDocumentAsinRelationsResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}

	return &response, nil
}

// PostContentDocumentAsinRelations 提交内容文档与 ASIN 关系
func PostContentDocumentAsinRelations(spClient *client.SPAPIClient, contentReferenceKey string, marketPlaceId string, request PostContentDocumentAsinRelationsRequest) (*PostContentDocumentAsinRelationsResponse, error) {
	if contentReferenceKey == "" {
		return nil, fmt.Errorf("contentReferenceKey is required")
	}
	// 构建 API 路径
	path := fmt.Sprintf("/aplus/2020-11-01/contentDocuments/%s/asins", contentReferenceKey)
	// 构建 Query 参数
	queryParams := url.Values{}
	if marketPlaceId == "" {
		return nil, fmt.Errorf("marketPlaceId is required")
	}
	queryParams.Add("marketplaceId", marketPlaceId)
	// 调用 SP-API 请求
	resp, err := spClient.MakeRequest("POST", path, nil, request)
	if err != nil {
		return nil, fmt.Errorf("failed to post content document ASIN relations: %w", err)
	}
	// 解析响应数据
	var response PostContentDocumentAsinRelationsResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}

// ValidateContentDocumentAsinRelations 验证内容文档与 ASIN 关系
func ValidateContentDocumentAsinRelations(spClient *client.SPAPIClient, marketPlaceId string, request PostContentDocumentRequest, asinSet []string) (*ValidateContentDocumentAsinRelationsResponse, error) {
	// 构建 API 路径
	path := "/aplus/2020-11-01/contentAsinValidations"
	queryParams := url.Values{}
	if marketPlaceId == "" {
		return nil, fmt.Errorf("marketPlaceId is required")
	}
	queryParams.Add("marketplaceId", marketPlaceId)
	if len(asinSet) > 0 {
		for _, asin := range asinSet {
			queryParams.Add("asinSet", asin)
		}
	}
	// 调用 SP-API 请求
	resp, err := spClient.MakeRequest("POST", path, queryParams, request)
	if err != nil {
		return nil, fmt.Errorf("failed to validate content document ASIN relations: %w", err)
	}

	// 解析响应数据
	var response ValidateContentDocumentAsinRelationsResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}

	return &response, nil
}

// SearchContentPublishRecords 搜索内容文档发布记录
func SearchContentPublishRecords(spClient *client.SPAPIClient, marketPlaceId string, asin string, pageToken string) (*SearchContentPublishRecordsResponse, error) {
	// 构建 API 路径
	path := "/aplus/2020-11-01/contentPublishRecords"
	queryParams := url.Values{}
	if marketPlaceId != "" {
		queryParams.Add("marketplaceId", marketPlaceId)
	} else {
		return nil, fmt.Errorf("marketPlaceId is required")
	}
	if asin != "" {
		queryParams.Add("asin", asin)
	} else {
		return nil, fmt.Errorf("asin is required")
	}
	if pageToken != "" {
		queryParams.Add("pageToken", pageToken)
	} else {
		return nil, fmt.Errorf("pageToken is required")
	}
	// 调用 SP-API 请求
	resp, err := spClient.MakeRequest("GET", path, queryParams, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to search content publish records: %w", err)
	}
	// 解析响应数据
	var response SearchContentPublishRecordsResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}

// PostContentDocumentApprovalSubmission 提交内容文档的审批请求
func PostContentDocumentApprovalSubmission(spClient *client.SPAPIClient, contentReferenceKey string, marketPlaceId string) (*PostContentDocumentApprovalSubmissionResponse, error) {
	if contentReferenceKey == "" {
		return nil, fmt.Errorf("contentReferenceKey is required")
	}
	// 构建 API 路径
	path := fmt.Sprintf("/aplus/2020-11-01/contentDocuments/%s/approvalSubmissions", contentReferenceKey)
	// 构建 Query 参数
	queryParams := url.Values{}
	if marketPlaceId == "" {
		return nil, fmt.Errorf("marketPlaceId is required")
	}
	queryParams.Add("marketplaceId", marketPlaceId)
	// 调用 SP-API 请求
	resp, err := spClient.MakeRequest("POST", path, queryParams, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to post content document approval submission: %w", err)
	}
	// 解析响应数据
	var response PostContentDocumentApprovalSubmissionResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}

	return &response, nil
}

// PostContentDocumentSuspendSubmission 提交内容文档的挂起请求
func PostContentDocumentSuspendSubmission(spClient *client.SPAPIClient, contentReferenceKey string, marketPlaceId string) (*PostContentDocumentSuspendSubmissionResponse, error) {
	if contentReferenceKey == "" {
		return nil, fmt.Errorf("contentReferenceKey is required")
	}
	// 构建 API 路径
	path := fmt.Sprintf("/aplus/2020-11-01/contentDocuments/%s/suspendSubmissions", contentReferenceKey)
	// 构建 Query 参数
	queryParams := url.Values{}
	if marketPlaceId == "" {
		return nil, fmt.Errorf("marketPlaceId is required")
	}
	queryParams.Add("marketplaceId", marketPlaceId)
	// 调用 SP-API 请求
	resp, err := spClient.MakeRequest("POST", path, queryParams, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to post content document suspend submission: %w", err)
	}
	// 解析响应数据
	var response PostContentDocumentSuspendSubmissionResponse
	if err := json.Unmarshal(resp.Body(), &response); err != nil {
		return nil, fmt.Errorf("failed to parse response: %w", err)
	}
	return &response, nil
}
